package net.vinote.smart.platform.module.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;

import net.vinote.smart.platform.module.log.RunLogger;

public class DBTransaction {
	private Connection conn;

	public DBTransaction(Connection conn) {
		this.conn = conn;
	}

	public void join(DBManager bm) {
		bm.setConn(conn);
	}

	public void commit() throws SQLException {
		if (!conn.getAutoCommit()) {
			RunLogger.getLogger().log(Level.FINEST, "commit transaction!");
			conn.commit();
		}
	}

	public void rollback() throws SQLException {
		conn.rollback();
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public void close() throws SQLException {
		if (conn != null) {
			conn.close();
			conn = null;
		}
	}

}
